package com.ruoyi.app.activity

import android.app.Activity
import android.content.Context
import android.content.Intent
import android.text.TextUtils
import android.view.View
import com.bumptech.glide.Glide
import com.drake.net.Get
import com.drake.net.Post
import com.drake.net.utils.scopeDialog
import com.drake.net.utils.scopeNetLife
import com.hjq.toast.ToastUtils
import com.ruoyi.app.MainActivity
import com.ruoyi.app.api.ConfigApi
import com.ruoyi.app.api.OKHttpUtils
import com.ruoyi.app.databinding.ActivityRegisterBinding
import com.ruoyi.app.model.entity.CaptchaImageEntity
import com.ruoyi.app.model.entity.LoginEntity
import com.ruoyi.app.model.request.LoginRequest
import com.ruoyi.app.model.request.registerRequest
import com.ruoyi.code.Frame
import com.ruoyi.code.base.BaseBindingActivity
import com.ruoyi.code.witget.toast
import com.tencent.mmkv.MMKV

class RegisterActivity : BaseBindingActivity<ActivityRegisterBinding>() {

    companion object {

        fun startActivity(context: Context) {
            val intent = Intent(context, RegisterActivity::class.java)
            if (context !is Activity) {
                intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
            }
            context.startActivity(intent)
        }

    }

    private var uuid: String? = ""

    override fun initView() {
        binding.tvLogin.setOnClickListener {
//            LoginActivity.startActivity(this)
            finish()
        }
        binding.btnRegister.setOnClickListener {
            val code = binding.etCode.text.toString().trim()
            if (binding.llCaptchaImage.visibility == View.VISIBLE) {
                if (TextUtils.isEmpty(code)) {
                    toast("请输入验证码")
                    return@setOnClickListener
                }
            }
            val username = binding.etAccount.text.toString().trim()
            if (TextUtils.isEmpty(username)) {
                toast("请输入账号")
                return@setOnClickListener
            }

            val password = binding.etPassword.text.toString().trim()
            if (TextUtils.isEmpty(password)) {
                toast("请输入密码")
                return@setOnClickListener
            }
            val rpassword = binding.etRPassword.text.toString().trim()
            if (TextUtils.isEmpty(rpassword)) {
                toast("请输入重复密码")
                return@setOnClickListener
            }
            if (!rpassword.equals(password)) {
                toast("两次输入的密码不一致")
                return@setOnClickListener
            }

            scopeDialog {
                val data = Post<LoginEntity>(ConfigApi.register) {
                    body = OKHttpUtils.getRequestBody(
                        registerRequest(
                            code,
                            uuid ?: "",
                            password,
                            rpassword,
                            username
                        )
                    )
                }.await()
                if (data.code == ConfigApi.SUCESSS) {
//                    LoginActivity.startActivity(this@RegisterActivity)
                    finish()
                } else {
                    ToastUtils.show(data.msg)
                    getVerificationCode()
                }
            }.catch {
                ToastUtils.show(it.message)
                getVerificationCode()
            }
        }
    }

    override fun initData() {
        getVerificationCode()
    }

    private fun getVerificationCode() {
        scopeNetLife {
            val data = Get<CaptchaImageEntity>(ConfigApi.getCaptchaImage).await()
            if (data.code == ConfigApi.SUCESSS) {
                val captchaEnabled = data.captchaEnabled
                if (captchaEnabled) {
                    binding.llCaptchaImage.visibility = View.VISIBLE
                    uuid = data.uuid
                    Glide.with(Frame.getContext())
                        .load("data:image/gif;base64,${data.img}")
                        .into(binding.logoCodeImage)
                } else {
                    binding.llCaptchaImage.visibility = View.GONE
                }
            } else {
                toast(data.msg)
            }
        }
    }


}